﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class AB55PR : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        AB55PRData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        
        #endregion
        public AB55PR()
        {
            InitializeComponent();
            this.Name = "机台生产单";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = true;
            dicParamater["DocType"] = "PBORDER";
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new AB55PRData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                InitDataBinding();
                /*
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct B.LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + "　A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI, A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS DIORDER "
                    + " FROM ABPOS A,LIEFDAT B,ABAUFM C "
                    + " WHERE A.ABPMANDANT = B.LSMANDANT AND A.ABPLIEFNR = B.LSLIEFNR "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAAUFLFD AND A.ABPAUFPOS = C.ABAAUFPOS "
                    + " AND A.ABPAUFART='PR'  AND A.ABPERLKZ <>'J' AND C.ABAAPOS='0001' "
                 ;
                */
                DataSet setResult = new DataSet();
                setResult = m_Data.LoadData(); 
                setUI.Merge(setResult);  
                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtABPAUFKRS.Text = dwABPOS["ABPAUFKRS"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPAUFLFD"].ToString();
                this.txtABPAUFPOS.Text = dwABPOS["ABPAUFPOS"].ToString();


                string order = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                dwABPOS["REMARK"] = m_Data.GetCommentsFromTXTIND("ABP", order);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;
        }


        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            dwTmp.ItemArray = dwABPOS.ItemArray;
            DataSet setRpt = new DataSet();
            setRpt = m_Data.BuildAB55PRData(setRpt, dwABPOS);
            //dwABPOS.ItemArray = dwTmp.ItemArray;
            dicParamater["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
            dicParamater["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
            dicParamater["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
            m_DataX = new AB55PRData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            AB55PRReport m_rpt = new AB55PRReport(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetReport("EXCEL"); 
        }


    }
}
